
*------------------------------------------------------------------------------*
* Minister position
*
* Additional specs:
*	1. Unweighted mean if ministers from two or more parties
*	2. Fallback 1: Government position weighted by seats
*	3. Fallback 2: 1st chamber median for expert governments
*
* Version:	2016-06-10
* Changes:	2016-??-??: First release
*------------------------------------------------------------------------------*


* Parse minister
local minister "$minister"
*di "`minister'"
tokenize "`minister'", parse(",")
local minister1 `1'
local minister2 `3'
local minister3 `5'
local minister4 `7'
local minister5 `9'
*di "`minister1'"
*di "`minister2'"
*di "`minister3'"
*di "`minister4'"
*di "`minister5'"


* Position of 1st minister(s)
gen zs1 = $ideology if p103 == 1 & `minister1' > 0
by g101 g105, sort: egen zs2=mean(zs1)

* Position of 2nd minister(s)
capture: gen zs3 = $ideology if p103 == 1 & `minister2' > 0
capture: by g101 g105, sort: egen zs4=mean(zs3)

* Position of 3rd minister(s)
capture: gen zs5 = $ideology if p103 == 1 & `minister3' > 0
capture: by g101 g105, sort: egen zs6=mean(zs5)

* Position of 4th minister(s)
capture: gen zs7 = $ideology if p103 == 1 & `minister4' > 0
capture: by g101 g105, sort: egen zs8=mean(zs7)

* Position of 5th minister(s)
capture: gen zs9 = $ideology if p103 == 1 & `minister5' > 0
capture: by g101 g105, sort: egen zs10=mean(zs9)


* Fill in GOV_POS
gen GOV_POS = .
replace GOV_POS = zs2  if GOV_POS == .
capture: replace GOV_POS = zs4  if GOV_POS == .
capture: replace GOV_POS = zs6  if GOV_POS == .
capture: replace GOV_POS = zs8  if GOV_POS == .
capture: replace GOV_POS = zs10 if GOV_POS == .


* Fallback 1: Government position weighted by seats
gen filterseat = p303 if p103 == 1 & $ideology != .	// Exclude NONAs and parties with missing ideology
by g101 g105, sort: egen newsumseat=sum(filterseat)	// Estimate new share
gen newpofseat = (filterseat / newsumseat * 100)
gen zs11 = ($ideology * newpofseat) / 100			// Weigh position with new share
by g101 g105, sort: egen zs12 = sum(zs11)			// Aggregate
save zs, replace


* Fill in GOV_POS with fallbacks
replace GOV_POS = zs12 if GOV_POS == .
replace GOV_POS = median1st if newsumseat == 0 & GOV_POS == 0


* Cleansing
replace GOV_POS = . if g106 == 9

lab var GOV_POS "Position of government"

sort g101 g105 p101

capture: macro drop minister
capture: drop zs* filterseat newsumseat newpofseat 
capture: erase zs.dta
capture: erase zs_median.dta
